/*
 * \brief  Server-side de-marshaller of cpu session interface
 * \author Christian Helmuth
 * \date   2006-07-12
 */

/*
 * Copyright (C) 2006-2009 Christian Helmuth
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _INCLUDE__CPU_SESSION__SERVER_H_
#define _INCLUDE__CPU_SESSION__SERVER_H_

#include <base/server.h>
#include <cpu_session/cpu_session.h>
#include <base/printf.h>

namespace Genode {

	class Cpu_session_server : public Server_object, public Cpu_session
	{
		public:

			int dispatch(int op, Ipc_istream &is, Ipc_ostream &os)
			{
				switch (op) {

				case CREATE_THREAD:
					{
						Buffer name;

						is >> name;
						try {
							os << create_thread(name.addr());
							return 0;

						} catch (Thread_creation_failed) {
							os << Thread_capability();
							return -1;
						}
					}

				case KILL_THREAD:
					{
						Thread_capability thread;

						is >> thread;
						kill_thread(thread);

						return 0;
					}

				case FIRST:
					{
						os << first();
						return 0;
					}

				case NEXT:
					{
						Thread_capability curr_thread;
						is >> curr_thread;
						os << next(curr_thread);
						return 0;
					}

				case SET_PAGER:
					{
						Thread_capability thread, pager;

						is >> thread >> pager;
						return set_pager(thread, pager);
					}

				case START:
					{
						Thread_capability thread;
						addr_t ip = 0, sp = 0;

						is >> thread >> ip >> sp;
						return start(thread, ip, sp);
					}

				case CANCEL_BLOCKING:
					{
						Thread_capability thread;
						is >> thread;
						cancel_blocking(thread);
						return 0;
					}

				case NAME:
					{
						Thread_capability thread;
						is >> thread;
						char name_buf[THREAD_NAME_LEN];
						int res = name(thread, name_buf, sizeof(name_buf));
						os << Buffer(name_buf);
						return res;
					}

				case STATE:
					{
						Thread_capability thread;
						Thread_state thread_state;
						is >> thread;
						int res = state(thread, &thread_state);
						os << thread_state;
						return res;
					}

				default:
					PWRN("unknown opcode %d", op);
					return -1;
				}
			}
	};
}

#endif /* _INCLUDE__CPU_SESSION__SERVER_H_ */
